﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using RKConsulting.Helpers;

namespace RKConsulting.Web.Helpers
{
    public static class WebControlHelper
    {
        public static void BindEnumValues<TEnum>(this DropDownList dropDownList) where TEnum: struct
        {
            dropDownList.DataSource = EnumHelper.GetAll<TEnum>();
            dropDownList.DataTextField = "Value";
            dropDownList.DataValueField = "Key";
            dropDownList.DataBind();
        }

        public static void SelectByText(this DropDownList dropDownList, string text)
        {
            var item = dropDownList.Items.Cast<ListItem>().FirstOrDefault(i => i.Text == text);
            if (item != null)
                item.Selected = true;
        }
    }
}
